// model/Post.js
import { Model} from '@nozbe/watermelondb'
import { Associations } from '@nozbe/watermelondb/Model';
import { field, date,text,relation, immutableRelation,children,writer  } from '@nozbe/watermelondb/decorators'

export default class Post extends Model {
  static table = 'posts'
  static associations: Associations = {
    comments: { type: 'has_many', foreignKey: 'post_id' },
  };
  @text('title') title:string;
  @date('last_event_at') lastEventAt:string;
  @text('body') body:string;
  @field('is_pinned') isPinned: boolean;
  @children('comments') comments; 
}


class Comment extends Model {
    static table = 'comments'
    static associations:Associations = {
      posts: { type: 'belongs_to', key: 'post_id' },
    }
    @field('is_spam') isSpam:boolean;
    @text('body') body:string;
    @relation('posts', 'post_id') post
  @immutableRelation('users', 'author_id') author

  @writer async markAsSpam() {
    await this.update(comment => {
      comment.isSpam = true
    })
  }


}